package com.microsoft.socialphoto.ui;

import java.util.ArrayList;
import java.util.List;

import org.odata4j.consumer.ODataConsumer;
import org.odata4j.core.OEntity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.microsoft.socialphoto.R;
import com.microsoft.socialphoto.RequestCode;
import com.microsoft.socialphoto.SocialActivity;
import com.microsoft.socialphoto.adapter.LazyAdapter;
import com.microsoft.socialphoto.adapter.item.PhotoItem;
import com.microsoft.socialphoto.odata.OData;

public class TopRated extends SocialActivity implements OnClickListener {

	private GridView gvPhoto;
	private LazyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_toprate);

		adapter = new LazyAdapter(this, R.id.btn_login, getPhotoData());
		gvPhoto = (GridView) findViewById(R.id.photo_list);
		gvPhoto.setOnItemClickListener(new PhotoOnClick());
		gvPhoto.setAdapter(adapter);

		findViewById(R.id.camera).setOnClickListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {
			if (requestCode == RequestCode.UPLOAD_PHOTO) {
				adapter = new LazyAdapter(this, R.id.btn_login, getPhotoData());
				gvPhoto.setAdapter(adapter);
			}
		}
	}

	private static ArrayList<Object> getPhotoData() {
		ArrayList<Object> list = new ArrayList<Object>();
		ODataConsumer c = OData.getInstance();

		List<OEntity> photos = c.getEntities("Photos").execute().toList();

		for (OEntity photo : photos) {
			PhotoItem item = new PhotoItem();

			item.setId(photo.getProperty("Id", Integer.class).getValue());
			item.setName(photo.getProperty("FileName", String.class).getValue());
			list.add(item);
		}

		return list;

	}

	private class PhotoOnClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int pos,
				long id) {
			PhotoItem item = (PhotoItem) adapterView.getItemAtPosition(pos);

			Intent i = new Intent(TopRated.this, PhotoDetail.class);
			i.putExtra("photo_id", item.getId());

			startActivity(i);
		}

	}

	@Override
	public void onDestroy() {
		adapter.imageLoader.stopThread();
		gvPhoto.setAdapter(null);
		super.onDestroy();
	}

	public OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			adapter.imageLoader.clearCache();
			adapter.notifyDataSetChanged();
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.camera:
			startActivityForResult(new Intent(this, UploadPhoto.class),
					RequestCode.UPLOAD_PHOTO);
			break;
		}
	}
}
